package cn.nbt.busManage.service.impl;

import cn.nbt.busManage.model.dto.NewsDto;
import cn.nbt.busManage.model.entity.News;
import cn.nbt.busManage.dao.repository.NewsRepository;
import cn.nbt.busManage.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@SuppressWarnings("JavadocDeclaration")
@Service
@Transactional

public class INewsService implements NewsService {

    @Autowired
    private NewsRepository newsRepository;

    /**
     * 查询所有新闻
     */
    @Override
    public List<NewsDto> findAll() {
        List<News> all = newsRepository.findAll();
        List<NewsDto> newsDtos = new ArrayList<>();
        for (News news : all){
            NewsDto newsDto = new NewsDto(news);
            newsDtos.add(newsDto);
        }
        return newsDtos;
    }

    /**
     * 根据作者名称查询新闻
     * @param author
     * @return
     */
    @Override
    public List<NewsDto> findByQuery(String author) {
        List<News> news = newsRepository.findByAuthor(author);
        List<NewsDto> newsDtos = new ArrayList<>();
        for (News newss : news){
            NewsDto newsDto = new NewsDto(newss);
            newsDtos.add(newsDto);
        }
        return newsDtos;
    }

    /**
     * 删除新闻信息
     * @param id
     */
    @Override
    public  void delete(Integer id) { newsRepository.deleteById(id);}

    /**
     * 增加新闻信息
     * @param newsDto 新闻信息
     */
    @Override
    public  void add (NewsDto newsDto){
        News news = new News();
        news.setId(newsDto.getId());
        news.setTitle(newsDto.getTitle());
        news.setAuthor(newsDto.getAuthor());
        news.setContent(newsDto.getContent());
        news.setCreateTime(LocalDateTime.now());
        news.setUpdateTime(null);
        newsRepository.save(news);
    }

    /**
     * 更新新闻信息
     * @param newsDto 新闻信息
     */
    @Override
    public void update(NewsDto newsDto){
        News news = new News();
        news.setId(newsDto.getId());
        news.setTitle(newsDto.getTitle());
        news.setAuthor(newsDto.getAuthor());
        news.setContent(newsDto.getContent());
        Optional<News> byId = newsRepository.findById(news.getId());
        if(byId.isEmpty()){
            throw new RuntimeException("新闻不存在");
        }
        news.setCreateTime(byId.get().getCreateTime());
        news.setUpdateTime(LocalDateTime.now());
        newsRepository.updateNews(news.getAuthor(),news.getTitle(), news.getContent(), news.getCreateTime(),news.getUpdateTime(), news.getId());
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @Override
    public NewsDto findById(Integer id) {
        Optional<News> byId = newsRepository.findById(id);
        if(byId.isEmpty()){
            throw new RuntimeException("该新闻不存在");
        }
        NewsDto newsDto = new NewsDto(byId.get());
        return newsDto;
    }
}
